package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.ApiTestPreCase;
import com.jmb.api.service.ApiTestPreCaseService;
import com.jmb.api.mapper.ApiTestPreCaseMapper;
import com.jmb.api.service.ApiTestService;
import com.jmb.api.vo.api.ApiTestCaseVo;
import com.jmb.api.vo.api.ApiTestPreCaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author jinbo
* @description 针对表【api_test_pre_case(前置用例表)】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class ApiTestPreCaseServiceImpl extends ServiceImpl<ApiTestPreCaseMapper, ApiTestPreCase>
    implements ApiTestPreCaseService{

    @Autowired
    private ApiTestPreCaseMapper apiTestPreCaseMapper;
    @Autowired
    private ApiTestService apiTestService;
    @Override
    public void savePreCase(ApiTestPreCaseVo apiTestPreCaseVo) {
        if (apiTestService.getApiTestCount(apiTestPreCaseVo.getPreCaseId()) == 0){
            throw new LeaseException(ResultCodeEnum.WEB_API_CASEID_NOT);
        }
        Integer preCaseId = apiTestPreCaseVo.getPreCaseId();
        Integer parentCaseId = apiTestPreCaseVo.getParentCaseId();
        if (Objects.equals(preCaseId, parentCaseId)){
            throw new LeaseException(ResultCodeEnum.WEB_API_PRE_CASE);
        }
        this.save(apiTestPreCaseVo);
    }

    @Override
    public void editPreCase(ApiTestPreCaseVo apiTestPreCaseVo) {
        // 查询caseId是否存在，如果不存在则提示caseId不存在
        apiTestService.selectByCaseId(apiTestPreCaseVo.getPreCaseId());
        // 检验是否为自身
        if (Objects.equals(apiTestPreCaseVo.getPreCaseId(), apiTestPreCaseVo.getParentCaseId())){
            throw new LeaseException(ResultCodeEnum.WEB_API_PRE_CASE);
        }
        LambdaUpdateWrapper<ApiTestPreCase> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApiTestPreCase::getParentCaseId,apiTestPreCaseVo.getParentCaseId());
        apiTestPreCaseMapper.update(apiTestPreCaseVo,updateWrapper);
    }

    /**
     * 查询用例的前置用例编号集合
     * @param caseId 用例编号
     * @return 前置用例集合
     */
    @Override
    public List<Integer> selectAllPreCaseId(Integer caseId) {
        LambdaQueryWrapper<ApiTestPreCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestPreCase::getParentCaseId,caseId);
        return apiTestPreCaseMapper.selectList(queryWrapper).stream().map(ApiTestPreCase::getPreCaseId).toList();

    }

    /**
     * 根据前置用例编号查询所有前置用例
     * @param preCaseId 用例编号
     * @return 返回的前置用例集合
     */
    @Override
    public List<ApiTestPreCase> getPreCaseId(Integer preCaseId) {
        if (preCaseId == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_CASEID_NOT);
        }
        LambdaQueryWrapper<ApiTestPreCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestPreCase::getPreCaseId,preCaseId)
                .eq(ApiTestPreCase::getStatusId,0);
        return this.list(queryWrapper);
    }
    /**
     * 查询用例的前置用例关联表自增编号集合
     * @param PreId 用例编号
     * @return 前置用例的集合
     */
    @Override
    public List<Integer> findApiTestPreCaseById(Integer PreId) {
        LambdaQueryWrapper<ApiTestPreCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestPreCase::getParentCaseId,PreId);
        return this.list(queryWrapper).stream().map(ApiTestPreCase::getId).toList();
    };

    /**
     * 递归获取所有前置用例(含前置用例包含的前置用例）集合
     * @param returnResult 用来接收返回数据的集合
     * @param PreCaseId 父用例编号
     * @return 取所有的前置用例（含前置用例包含的前置用例）集合
     */
    @Override
    public List<Integer> recursionPreCase(List<Integer> returnResult, Integer PreCaseId) {
        List<Integer> list = this.findApiTestPreCaseById(PreCaseId);
        returnResult.addAll(list);
        for (Integer pId : list) {
            List<Integer> integers = recursionPreCase(new ArrayList<>(), pId);
            returnResult.addAll(integers);
        }
        return returnResult;
    }

    /**
     * 删除指定用例的前置用例
     * @param id 前置用例自增编号
     */
    @Override
    public void removeApiTestPreCase(Integer id) {
        if (id == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        this.removeById(id);

    }
    /**
     * 删除所有的前置用例
     * @param preCaseId 父用例ID
     */
    @Override
    public void removeApiTestPreCaseByCaseId(Integer preCaseId) {
        LambdaQueryWrapper<ApiTestPreCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestPreCase::getParentCaseId,preCaseId);
        this.remove(queryWrapper);
    }
}




